package com.ssy.lingxi.platform.template.service.serviceimpl.adorn;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.constant.template.PageTemplateTypeEnum;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.template.service.constant.CommonBooleanEnum;
import com.ssy.lingxi.platform.template.service.constant.PageTemplateShelfEnum;
import com.ssy.lingxi.platform.template.service.entity.adorn.AppAdorn;
import com.ssy.lingxi.platform.template.service.entity.adorn.PageTemplate;
import com.ssy.lingxi.platform.template.service.model.dto.adorn.AppAdornDTO;
import com.ssy.lingxi.platform.template.service.model.dto.common.TemplateIdDTO;
import com.ssy.lingxi.platform.template.service.repository.AppAdornRepository;
import com.ssy.lingxi.platform.template.service.repository.PageTemplateRepository;
import com.ssy.lingxi.platform.template.service.service.adorn.AppChannelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 模板装修 - APP渠道装修 - 业务实现层
 *
 * @author fhj
 * @version 2.0.0
 * @date 2020/12/11
 */
@Slf4j
@Service
public class AppChannelServiceImpl implements AppChannelService {

    @Resource
    private PageTemplateRepository pageTemplateRepository;
    @Resource
    private AppAdornRepository appAdornRepository;

    @Override
    @Transactional
    @ApiOperation(value = "保存APP渠道商城装修")
    public Wrapper<Void> save(AppAdornDTO dto, UserLoginCacheDTO user) {
        //获取渠道模板且校验
        PageTemplate pageTemplate = pageTemplateRepository.findById(dto.getTemplateId()).orElse(null);
        if (pageTemplate == null) {
            return Wrapper.fail(ResponseCode.PT_NO_MATCHING_TEMPLATE_FOUND);
        }
        if (!pageTemplate.getType().equals(PageTemplateTypeEnum.CHANNEL.getCode())) {
            return Wrapper.fail(ResponseCode.PT_THIS_TEMPLATE_TYPE_IS_NOT_A_CHANNEL_TEMPLATE);
        }
        if (pageTemplate.getAllow().equals(CommonBooleanEnum.NO.getCode())) {
            return Wrapper.fail(ResponseCode.PT_TEMPLATE_NOT_ALLOWED);
        }
        if (pageTemplate.getShelf().equals(PageTemplateShelfEnum.OFF_SHELF.getCode())) {
            return Wrapper.fail(ResponseCode.PT_TEMPLATE_HAS_BEEN_REMOVED);
        }

        //获取当前登录渠道会员的APP渠道模板装修
        AppAdorn appAdorn = appAdornRepository.findByTemplateIdAndMemberIdAndRoleId(
                dto.getTemplateId(),
                user.getMemberId(),
                user.getMemberRoleId()
        );

        //保存APP渠道模板装修
        appAdorn = appAdorn == null ? new AppAdorn() : appAdorn;
        appAdorn.setTemplateId(dto.getTemplateId());
        appAdorn.setMemberId(user.getMemberId());
        appAdorn.setRoleId(user.getMemberRoleId());
        appAdorn.setAdornContent(CollectionUtil.isNotEmpty(dto.getAdornContent()) ? dto.getAdornContent() : appAdorn.getAdornContent());
        appAdorn.setCategoryAdornContent(CollectionUtil.isNotEmpty(dto.getCategoryAdornContent()) ? dto.getCategoryAdornContent() : appAdorn.getCategoryAdornContent());
        appAdornRepository.saveAndFlush(appAdorn);
        return Wrapper.success();
    }

    @Override
    @ApiOperation(value = "获取APP渠道商城装修")
    public Wrapper<AppAdorn> find(TemplateIdDTO dto, UserLoginCacheDTO user) {
        return Wrapper.success(
                appAdornRepository.findByTemplateIdAndMemberIdAndRoleId(
                        dto.getTemplateId(),
                        user.getMemberId(),
                        user.getMemberRoleId()
                )
        );
    }
}
